﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GomeeSoft.src.classes;
using GomeeSoft.src.classes.DiaryPages;

namespace GomeeSoft.src.form.DiaryPages
{
    public partial class UCMonthR : UserControl
    {        
        // Varibles -----=======================================================================
        #region Varibles
        private int year = DateTime.Now.Year;
        public int Year
        {
            get { return year; }
            set { year = value; SetDateType(); }
        }
        private int month = DateTime.Now.Month;
        public int Month
        {
            get { return month; }
            set { month = value; SetDateType(); }
        }
        private Size cellRange;
        private UCDayPanel[,] days = new UCDayPanel[5, 4];
        #endregion

        // Initialize ==========================================================================
        #region Initialize
        public UCMonthR()
        {
            InitializeData();
            InitializeComponent();
            InitializeDesign();
        }
        private void InitializeDesign()
        {
            BackColor = Color.White;
        }
        private void InitializeData()
        {
            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    days[row, col] = new UCDayPanel();
                    this.Controls.Add(days[row,col]);
                }
            }
        }
        #endregion

        // OnResize ============================================================================
        #region OnResize
        private void UCMonthR_Resize(object sender, EventArgs e)
        {
            cellRange = new Size((int)Math.Floor((double)(Width - 7) / 4), (int)Math.Floor((double)(Height - 90) / 5));

            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    days[row, col].Left = (cellRange.Width + 1) * col + 2;
                    days[row, col].Top = 78 + (cellRange.Height + 1) * row;
                    days[row, col].Size = cellRange;
                }
            }
            Invalidate();
        }
        #endregion

        // Paint ===============================================================================
        #region Paint
        private void UCMonthR_Paint(object sender, PaintEventArgs e)
        {
            UCPage.PaintHeaderLine(sender, e, new Size(Width, Height));
            PaintCalendarLines(sender, e);
        }
        private void PaintCalendarLines(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Global.GetColor(Global.MonthPageCalendarBorderColor[0]));
            Point p1 = new Point(0, 57);
            Point p2 = new Point(cellRange.Width * 4 + 6, 57);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, 20);
            p2.Offset(0, 20);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height + 1);
            p2.Offset(0, cellRange.Height + 1);
            e.Graphics.DrawLine(pen, p1, p2);
            p1 = new Point(0, 57);
            p2 = new Point(p1.X, p1.Y + 26 + cellRange.Height * 5);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 2, 20);
            p2.Offset(cellRange.Width + 2, 0);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 1, 0);
            p2.Offset(cellRange.Width + 1, 0);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 1, 0);
            p2.Offset(cellRange.Width + 1, 0);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width + 1, 0);
            p2.Offset(cellRange.Width + 1, 0);
            e.Graphics.DrawLine(pen, p1, p2);

            pen = new Pen(Global.GetColor(Global.MonthPageCalendarBorderColor[1]));
            p1 = new Point(1, 76);
            p2 = new Point(cellRange.Width * 4 + 6, 76);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(0, cellRange.Height * 5 + 7);
            p2.Offset(0, cellRange.Height * 5 + 7);
            e.Graphics.DrawLine(pen, p1, p2);
            p1 = new Point(1, 76);
            p2 = new Point(p1.X, p1.Y + cellRange.Height * 5 + 6);
            e.Graphics.DrawLine(pen, p1, p2);
            p1.Offset(cellRange.Width * 4 + 5, -19);
            p2.Offset(cellRange.Width * 4 + 5, 0);
            e.Graphics.DrawLine(pen, p1, p2);

            SolidBrush b = new SolidBrush(Global.GetColor(Global.MonthPageCalendarBackColor));
            p1 = new Point(1, 58);
            Size s = new Size(cellRange.Width * 4 + 5, 18);
            e.Graphics.FillRectangle(b, new Rectangle(p1, s));
        }
        #endregion

        // Business Logic ======================================================================
        #region Business Logic
        private void UCMonthR_Load(object sender, EventArgs e)
        {
            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
        }

        private void SetDateType()
        {
            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    days[row, col].Date = Calendar.GetDateTime(Year, Month, row, col + 3);

                    if (days[row, col].Date.Month == Month)
                        days[row, col].IsCurrentMonth = true;
                    else
                        days[row, col].IsCurrentMonth = false;
                }
            }
        }
        #endregion
    }
}
